package by.epam.trenings.vkphotorest.adapters;

import by.epam.trenings.vkphotorest.R;
import by.epam.trenings.vkphotorest.app.PhotoGridActivity;
import by.epam.trenings.vkphotorest.content.contract.TaskContract;
import by.epam.trenings.vkphotorest.imageLoader.ImageLoaderCallback;
import by.epam.trenings.vkphotorest.utils.TaskApplication;
import by.epam.trenings.vkphotorest.utils.context.ContextHolder;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

public class PhotoGridAdapter extends CursorAdapter {

	public PhotoGridAdapter(Context context, Cursor c) {
		super(context, c, true);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	private LayoutInflater mInflater;
	private int mPlaceholder = R.drawable.ic_stub_image;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_grid_photos, null);
		}
		ImageView imageView = (ImageView) convertView
				.findViewById(R.id.view_grid_photos_image);
		if (!PhotoGridActivity.sBusy) {
			/** Preview photos */
			Cursor cursor = getCursor();
			cursor.moveToPosition(position);
			TaskApplication.getImageLoader().bind(
					cursor.getString(cursor
							.getColumnIndex(TaskContract.PhotosColumns.SRC)),
					imageView, new ImageLoaderCallback() {

						@Override
						public void onSucsess() {
						}

						@Override
						public void onError(Exception e) {

							String warning = ContextHolder
									.getInstance()
									.getContext()
									.getResources()
									.getString(
											R.string.adapter_album_strings_no_internet);

							Toast.makeText(
									ContextHolder.getInstance().getContext(),
									warning + " : " + e.getMessage(),
									Toast.LENGTH_SHORT).show();
						}
					});
			convertView.setTag(null);
		} else {
			imageView.setImageResource(mPlaceholder);
			convertView.setTag(this);
		}
		return convertView;
	}

	@Override
	public void bindView(final View view, Context context, Cursor cursor) {
		// final ViewHolder viewHolder = (ViewHolder) view.getTag();
		/*
		 * final ImageView imageView = (ImageView) view
		 * .findViewById(R.id.view_grid_photos_image);
		 * 
		 * if (!PhotoGridActivity.sBusy) {
		 * 
		 * TaskApplication.getImageLoader().bind( cursor.getString(cursor
		 * .getColumnIndex(TaskContract.PhotosColumns.SRC)), /*
		 * viewHolder.getImageView() imageView, new ImageLoaderCallback() {
		 * 
		 * @Override public void onSucsess() { imageView.setTag(null); }
		 * 
		 * @Override public void onError(Exception e) {
		 * 
		 * String warning = ContextHolder .getInstance() .getContext()
		 * .getResources() .getString(
		 * R.string.adapter_album_strings_no_internet);
		 * 
		 * Toast.makeText( ContextHolder.getInstance().getContext(), warning +
		 * " : " + e.getMessage(), Toast.LENGTH_SHORT).show();
		 * imageView.setTag(null); } }); } else { /* viewHolder.getImageView()
		 * imageView .setImageResource(mPlaceholder); imageView.setTag(this); }
		 */
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = View.inflate(context, R.layout.view_grid_photos, null);
		return view;
	}
}